package com.zktr.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.zktr.bean.AdminBean;
import com.zktr.pojo.Admin;
import com.zktr.service.AdminService;

/**
 * @author yf  
 * @version 创建时间：2018年8月26日  下午1:45:33
 * tags
 */
@WebServlet("/AdminServlet")
public class AdminServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	/**
	 * 登录
	 * @param req
	 * @param res
	 * @throws IOException 
	 */
	public void loginVerification(HttpServletRequest req,HttpServletResponse res) throws IOException {
		PrintWriter out = res.getWriter();
		AdminService aService = new AdminService();
		String username = req.getParameter("name");
		String password = req.getParameter("password");
		List<AdminBean> flag=aService.loginVerification(username, password);
		if (flag.size()!=0) {
			HttpSession session = req.getSession();
			session.setAttribute("admin",flag);
			out.print(true);
		}
	}
	/**
	 * 管理员登录名验证
	 * @param req
	 * @param res
	 */
	public void loginname(HttpServletRequest req,HttpServletResponse res) {
		AdminService aService = new AdminService();
		String username = req.getParameter("name");
		boolean flag=aService.loginVerificationUsername(username);
		try {
			PrintWriter out = res.getWriter();
			out.print(flag);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 验证码验证
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String ajaxValidateVerifyCode(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String verifyCode = req.getParameter("code");
		String vcode = (String) req.getSession().getAttribute("vCode");
		boolean b = verifyCode.equalsIgnoreCase(vcode);
		resp.getWriter().print(b);
		return null;
	}
	/**
	 * 查询用户所有信息
	 * @param req
	 * @param res
	 * @throws IOException 
	 */
	public void queryAllAdmin(HttpServletRequest req, HttpServletResponse res) throws IOException {
		PrintWriter out = res.getWriter();
		AdminService aService = new AdminService();
		List<AdminBean> queryAdmin = aService.queryAllAdmin();
		HttpSession session = req.getSession();
		session.setAttribute("list", queryAdmin);
		out.println("0");
	}
	/**
	 * 新增管理员
	 * @param req
	 * @param res
	 * @throws IOException
	 */
	public String insertAdmin(HttpServletRequest req, HttpServletResponse res) throws IOException {
		AdminService aService = new AdminService();
		PrintWriter out = res.getWriter();
		String name = req.getParameter("username");
		System.out.println("dd"+name);
		String email = req.getParameter("email");
		String password = req.getParameter("password");
		Date date = new Date();
	    long gmtMills = date.getTime() + 60000 * date.getTimezoneOffset();
		int x=(int) (gmtMills / 1000l);
		Admin admin = new Admin();
		admin.setUserid(x);
		admin.setUsername(name);
		admin.setPassword(password);
		admin.setEmail(email);
		admin.setAddtime(x);
		admin.setLastlogin(admin.getLastlogin());
		admin.setRolelist("管理员");
		admin.setRoleid(admin.getRoleid());
		admin.setPortrait("../img/lqd.jpg");
	    int row = aService.queryAllAdmin(admin);
	    if (row>0) {
			System.out.println("更新成功");
			return "r:/yf/fileSuccessful.jsp";
		}else {
			System.out.println("添加失败");
		}
	    return "r:/lyj/index.jsp";
	}
	public void  updateAdmin(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
		AdminService aService = new AdminService();
		AdminBean aBean = new AdminBean();
		PrintWriter out = res.getWriter();
		String id = req.getParameter("userid");
		SmartUpload smartUpload = new SmartUpload();
		ServletConfig config = this.getServletConfig();
		smartUpload.initialize(config, req, res);
		try {
			smartUpload.upload();
			com.jspsmart.upload.File smartFile = smartUpload.getFiles().getFile(0);
			String name = smartFile.getFilePathName();
			String path= req.getSession().getServletContext().getRealPath("/AdminServlet");
		    String replaceAll = path.replaceAll("AdminServlet", "img");
			String address=replaceAll +"\\"+ name;
			smartFile.saveAs(address);
			/*String endress=address.replaceAll("\\\\", "/");*/
			String relative="../img/"+name;
			aBean.setPortrait(relative);
			int row = aService.updateOnePortrait(aBean, id);
			System.out.println(row);
		} catch (SmartUploadException e) {
			e.printStackTrace();
		}
		out.print("0");
	}
	/**
	 * 添加管理员
	 * @param req
	 * @param res
	 * @throws IOException
	 */
	public void addAllAdmin(HttpServletRequest req,HttpServletResponse res) throws IOException {
		PrintWriter out = res.getWriter();
		out.println("0");
	}
	/**
	 * 删除管理员
	 * @param req
	 * @param res
	 * @throws IOException
	 */
	public void deleteAdmin(HttpServletRequest req,HttpServletResponse res) throws IOException {
		AdminService aService = new AdminService();
		PrintWriter out = res.getWriter();
        String id = req.getParameter("userid");
        boolean deleteOneAdmin = aService.deleteOneAdmin(Integer.parseInt(id));
		List<AdminBean> queryAdmin = aService.queryAllAdmin();
		HttpSession session = req.getSession();
		session.setAttribute("list", queryAdmin);
        if (deleteOneAdmin) {
			out.print("0");
		}else {
			out.print("1");
		}

	}
	
	/**
	 * 修改管理员信息
	 * @param req
	 * @param res
	 * @throws IOException
	 */
	public void updataAdmin(HttpServletRequest req,HttpServletResponse res) throws IOException {
		AdminService aService = new AdminService();
		PrintWriter out = res.getWriter();
		AdminBean aBean = new AdminBean();
		aBean.setEmail(req.getParameter("email"));
		aBean.setUsername(req.getParameter("username"));
		int row = aService.updateOneAll(aBean, req.getParameter("userid"));
		   if (row>0) {
				out.print("0");
			}else {
				out.print("1");
			}
      

	}
	/**
	 * 修改密码
	 * @param req
	 * @param res
	 * @throws IOException
	 */
	public void updataAdminpassword(HttpServletRequest req,HttpServletResponse res) throws IOException {
		AdminService aService = new AdminService();
		PrintWriter out = res.getWriter();
		AdminBean aBean = new AdminBean();
		aBean.setPassword(req.getParameter("password"));
		int row = aService.updateOneAll(aBean, req.getParameter("userid"));
		   if (row>0) {
				out.print("0");
			}else {
				out.print("1");
			}
      

	}
	
	
}